/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thesis.algorithm;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.LinkedList;

import com.thesis.utils.BusControl;
import com.thesis.utils.CONSTANTS;

/**
 * 
 * @author Aya Hikaru
 */
public class FileConverter {

	public LinkedList<Device> listOfDevices = new LinkedList<Device>();
	public LinkedList<Sequence> listOfSequences = new LinkedList<Sequence>();

	public void Clear() {
		listOfDevices.clear();
		listOfSequences.clear();
	}

	public FileConverter() {
		this(false);
	}

	public FileConverter(boolean readFromBinary) {
		if (!readFromBinary) {
			this.ReadFromtxt(CONSTANTS.TRAIN_DATA_FOLDER);
		} else {
			this.ReadFromBinary(CONSTANTS.SEQUENCE_BINARY_FILEPATH);
		}
	}
        
        public FileConverter(String FilePath)
        {
            this.ReadFromtxt(FilePath);
        }
	private void ReadFromtxt(String FilePath) {
		try {
			File file = new File(FilePath);
			if (file.isDirectory()) {
				File[] listOfFiles = file.listFiles();
				for (int i = 0; i < listOfFiles.length; i++) {
					if (listOfFiles[i].isFile() && listOfFiles[i].exists()
							&& listOfFiles[i].getName().endsWith("_header.txt")) {
						String generalFilePath = listOfFiles[i]
								.getAbsolutePath().substring(
										0,
										listOfFiles[i].getAbsolutePath()
												.length() - 11);
						String logfilePath = generalFilePath + "_log.txt";
						File log = new File(logfilePath);
						if (log.isFile() && log.exists()) {
							Device tmp = new Device();
							tmp.ReadFromtxt(generalFilePath);
							listOfDevices.add(tmp);
						}
					}
				}
			} else {
				if (file.isFile() && file.exists()
						&& file.getName().endsWith("_header.txt")) {
					String generalFilePath = file.getAbsolutePath().substring(
							0, file.getAbsolutePath().length() - 11);
					String logfilePath = generalFilePath + "_log.txt";
					File log = new File(logfilePath);
					if (log.isFile() && log.exists()) {
						Device tmp = new Device();
						tmp.ReadFromtxt(generalFilePath);
						listOfDevices.add(tmp);
					}
				}
			}
			// UpdateListOfSequences();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public void WriteToXML(String xmlFileDirectory) {
		try {
			File file = new File(xmlFileDirectory);
			if (file.isDirectory()) {
				for (Device d : listOfDevices) {
					d.WriteToXML(xmlFileDirectory);
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public void WriteToBinary() {
		WriteToBinary(CONSTANTS.SEQUENCE_BINARY_FILEPATH);
	}

	private void WriteToBinary(String FilePath) {
		try {
			File file = new File(FilePath);
			if (file.isDirectory()) {
				file = new File(CONSTANTS.SEQUENCE_BINARY_FILEPATH);
				System.out.println(FilePath
						+ " is illegal in FileConverter.WriteToBinary");
			}

			DataOutputStream out = new DataOutputStream(new FileOutputStream(
					file));

			BusControl busControl = new BusControl();
			LinkedList<Sequence> seqs = busControl
					.produceTrainSequence(listOfDevices);
			for (Sequence seq : seqs) {
				seq.writeBinaryShorten(out);
			}
			out.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

	private void ReadFromBinary(String FilePath) {
		File file = new File(FilePath);
		if (file.isDirectory()) {
			System.out.println("Wrong file path for binarysequencefile");
		} else {
			if (file.isFile() && file.exists()) {
				DataInputStream in;
				try {
					in = new DataInputStream(new FileInputStream(FilePath));
					this.ReadFromDataInputStream(in);
					in.close();
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
			} else {
				System.out.println("Illegal file path for binarysequencefile");
			}
		}
	}

	public void ReadFromDataInputStream(DataInputStream in) {
		Sequence stmp = null;
		SequenceElement setmp = new SequenceElement();

		this.Clear();
		try {
			char tmp;
			int count = -1;
			tmp = in.readChar();
			while (true) {
				stmp = new Sequence();
				if (tmp == CONSTANTS.MAXCHAR) {
					stmp.BusRouteID = in.readShort();
					tmp = in.readChar();
					while (tmp != CONSTANTS.MAXCHAR) {
						if (tmp == (char) 0) {
							count = 0;
							setmp = new SequenceElement();
							setmp.Observation = in.readChar();
							setmp.State = in.readChar();
							setmp.Lac = in.readChar();
							stmp.add(setmp);
						} else {
							count++;
							while (count < (int) tmp) {
								setmp = new SequenceElement(setmp.Observation,
										setmp.State, setmp.Lac);
								stmp.add(setmp);
								count++;
							}
							setmp = new SequenceElement();
							setmp.Observation = in.readChar();
							setmp.State = in.readChar();
							setmp.Lac = in.readChar();
							stmp.add(setmp);
						}
						tmp = in.readChar();
					}
					listOfSequences.add(stmp);
				} else {
					break;
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			// System.out.println(listOfSequences.size() + " is the size");
		} finally {
			if (stmp != null) {
				listOfSequences.add(stmp);
			}
		}

	}

}
